﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.CBO.SCM.Warehouse;
using UFIDA.U9.IssueNew.Enums;
using UFIDA.U9.SM.SO;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForMaterialUpdate : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //材料出库单
            UFIDA.U9.IssueNew.MaterialDeliveryDocBE.MaterialDeliveryDoc mater = key.GetEntity() as UFIDA.U9.IssueNew.MaterialDeliveryDocBE.MaterialDeliveryDoc;
            if (mater == null) return;

            #region 材料出库单提交时,校验库位,批次管理
            if (mater.OriginalData.DocState ==DocStateEnum.Opened && mater.DocState==DocStateEnum.Approving)
            {
                foreach (var line in mater.MaterialDeliveryDocLines)
                {
                    //引入料品
                    ItemMaster item = ItemMaster.Finder.FindByID(line.ItemInfo.ID);
                    //判断批号
                    if(item.InventoryInfo.LotParam != null && line.LotMaster==null )
                    {
                        throw new Exception($"该料品'[{line.ItemInfo.Code}]'启用了批次管理,请填写批号");
                    }
                    //引入存储地点
                    Warehouse wh = Warehouse.Finder.FindByID(line.SupplyWh.ID);
                    //判断库位
                    if (wh.IsBin == true && line.Bin ==null )
                    {
                        throw new Exception($"该仓库'[{line.SupplyWh.Name}]',启动了库位管理,请填写库位");
                    }
                }
            }
 



            #endregion

        }

    }
}
